// Evol functions.
// Author:
//    4144
// Description:
//    Functions for marriage

// check is player is near marriage npc
// args:
//   0 - player name
// returns:
//   true if player located near npc.
function	script	marriagecheckname	{
    .@name$ = getarg(0);
    if (.@name$ == "")
    {
        // no other registrand
        return false;
    }
    .@id = getcharid(0, .@name$);
    .@accoundId = getcharid(3, .@name$);
    if (isloggedin(.@accoundId, .@id) == false)
    {
        // registrant not logged in
        return false;
    }
    getmapxy(.@mapname$, .@x, .@y, 0, .@name$);
    if (.@mapname$ != strnpcinfo(4))
    {
        // registrant on other map
        return false;
    }
    if (distance(.@x, .@y, .x, .y) > .distance)
    {
        // registrant too far
        return false;
    }
    return true;
}

// return player name registered with same gender like attached player
function	script	getmarriageregistrant	{
    if (Sex)
    {
        .@name$ = getvariableofnpc(.maleName$, strnpcinfo(3));
    }
    else
    {
        .@name$ = getvariableofnpc(.femaleName$, strnpcinfo(3));
    }
    return .@name$;
}

// return registered marriage partner name
function	script	getmarriagepartner	{
    if (Sex)
    {
        .@name$ = getvariableofnpc(.femaleName$, strnpcinfo(3));
    }
    else
    {
        .@name$ = getvariableofnpc(.maleName$, strnpcinfo(3));
    }
    if (marriagecheckname(.@name$))
        return .@name$;
    return "";
}

// register attached player as partner for marriage
function	script	marriageregisterself	{
    if (Sex)
    {
        set getvariableofnpc(.maleName$, strnpcinfo(3)), strcharinfo(0);
        set getvariableofnpc(.maleName_partner$, strnpcinfo(3)), "";
    }
    else
    {
        set getvariableofnpc(.femaleName$, strnpcinfo(3)), strcharinfo(0);
        set getvariableofnpc(.femaleName_partner$, strnpcinfo(3)), "";
    }
    return;
}

// return player name what was accepted by partner
function	script	getmarriagepartneraccepted	{
    if (Sex)
    {
        .@name$ = getvariableofnpc(.femaleName_partner$, strnpcinfo(3));
    }
    else
    {
        .@name$ = getvariableofnpc(.maleName_partner$, strnpcinfo(3));
    }
    return .@name$;
}

// return true if partner present near and partner accepted you
function	script	ismarriagepartneraccepted	{
    .@partner$ = getmarriagepartner();
    if (.@partner$ == "")
        return false;
    .@name$ = getmarriagepartneraccepted();
    if (.@name$ == strcharinfo(0))
        return true;
    return false;
}

// accept for attached player his/her partner
// args:
//   0 - partner name
function	script	marriageacceptpartner	{
    .@name$ = getarg(0);
    if (Sex)
    {
        set getvariableofnpc(.maleName_partner$, strnpcinfo(3)), .@name$;
    }
    else
    {
        set getvariableofnpc(.femaleName_partner$, strnpcinfo(3)), .@name$;
    }
    return;
}

function	script	askmarry	{
    speech l("Do you want to marry @@?", getarg(0));
    if (askyesno() == ASK_YES)
        return true;
    return false;
}

// start marriage registration process
function	script	marriageregister	{
    .@partner$ = getmarriagepartner();
    if (.@partner$ == "")
    {   // no partner registered
        speech l("Ok I add your name... @@...", strcharinfo(0));
        marriageregisterself();
        next;
        speech l("Now wait your partner, and talk with me again");
        npctalk l("@@ registered for marriage. Waiting for partner...", strcharinfo(0));
        close;
    }
    else
    {   // partner already registered
        if (askmarry(.@partner$) == true)
        {
            marriageregisterself();
            marriageacceptpartner(.@partner$);
            npctalk l("@@ registered for marriage and accepted partner @@!", strcharinfo(0), .@partner$);
            npctalk l("Waiting for @@...", .@partner$);
            close;
        }
        else
        {
            close;
        }
    }
    return;
}

// remove all marriage registations
function	script	marriageclear	{
    set getvariableofnpc(.maleName$, strnpcinfo(3)), "";
    set getvariableofnpc(.femaleName$, strnpcinfo(3)), "";
    set getvariableofnpc(.maleName_partner$, strnpcinfo(3)), "";
    set getvariableofnpc(.femaleName_partner$, strnpcinfo(3)), "";
    return;
}

// do actual marriage
function	script	domarriage	{
    .@name$ = strcharinfo(0);
    .@partner$ = getarg(0);
    if (marriage(.@partner$))
    {
        speech l("You was married to @@!", .@partner$);
        npctalk l("@@ and @@ just married!", .@name$, .@partner$);
    }
    else
    {
        npctalk l("Marriage failed.");
    }
    marriageclear();
    return;
}

// marry main code
function	script	marriagemarry	{
    .@registrant$ = getmarriageregistrant();
    if (marriagecheckname(.@registrant$) == true)
    {
        if (.@registrant$ == strcharinfo(0))
        {
            if (ismarriagepartneraccepted())
            {
                .@partner$ = getmarriagepartner();
                if (marriagecheckname(.@partner$) == false)
                {
                    speech l("Partner not ready.");
                }
                else if (askmarry(.@partner$) == true)
                {
                    domarriage(.@partner$);
                }
            }
            else
            {
                speech l("You already registered. Wait for your partner.");
            }
        }
        else
        {
            speech l("Sorry I busy with other registrations"),
                l("Come after some time.");
        }
        close;
    }

    speech l("What a can do for you?");
    switch (select(l("I want register for marriage"),
            l("Nothing")))
    {
        case 1:
            marriageregister;
            break;
        case 2:
            break;
    }
    return;
}

// divorce main code
function	script	marriagedivorce	{
    speech l("What a can do for you?");
    switch (select(l("I want divorce"),
            l("Nothing")))
    {
        case 1:
            speech l("Are you sure?");
            if (askyesno() == ASK_YES)
            {
                if (divorce())
                {
                    speech l("Now you divorced!"),
                        l("Good look.");
                    npctalk l("@@ divorced!", strcharinfo(0));
                }
                else
                {
                    speech l("Divorce error!");
                }
            }
            break;
        case 2:
            break;
    }
    return;
}

// main function for marriage
function	script	marriagemain	{
    if (Sex > 1)
    {
        speech l("Sorry i can't help you. Go away!");
        close;
    }

    if (getpartnerid() != 0)
    {   // have partner
        marriagedivorce();
    }
    else
    {   // no partner
        marriagemarry();
    }

    return;
}

// check registration list by timer
function	script	marriagecheck	{
    .@name$ = getvariableofnpc(.maleName$, strnpcinfo(3));
    if (.@name$ != "" && marriagecheckname(.@name) == false)
    {
        set getvariableofnpc(.maleName$, strnpcinfo(3)), "";
        set getvariableofnpc(.maleName_partner$, strnpcinfo(3)), "";
    }
    .@name$ = getvariableofnpc(.femaleName$, strnpcinfo(3));
    if (.@name$ != "" && marriagecheckname(.@name) == false)
    {
        set getvariableofnpc(.femaleName$, strnpcinfo(3)), "";
        set getvariableofnpc(.femaleName_partner$, strnpcinfo(3)), "";
    }
}
